package com.henz4help.app;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.henz4help.app.base.BaseActivity;
import com.henz4help.app.channel.ChannelClient;
import com.henz4help.app.channel.GaeChannel;
import com.henz4help.app.common.HttpWorker;
import com.henz4help.app.common.Util;


public class GcmReportActivity extends BaseActivity {
	
	protected GaeChannel channel = null;
	
	private GcmReportActivity inst = null;
	
	private TextView vTxtMessage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		
		Util.Log("GcmReportActivity onOpen");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gcm_report_activity);
		inst = this;
		setComponent();
		try {
			setGAEChannel( inst );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void setComponent() {
		vTxtMessage = (TextView)findViewById(R.id.vTxtMessage);
		vTxtMessage.setText( "GcmReportActivity onOpen" );
		displayGAEMessage( "GcmReportActivity onOpen" );
	}
	
	
	protected void displayGAEMessage(String message){
		String dispMsg = vTxtMessage.getText().toString();
		dispMsg = message + "\r\n" + dispMsg;
		vTxtMessage.setText( dispMsg );
	}
	
	protected void channelOpened(){
		try {
			channel.send("Android Channel Opened");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void setGAEChannel( GcmReportActivity acti )throws Exception{
		
		ChannelWorker worker = null;
		worker = new ChannelWorker( acti );
		worker.start();
		/*
		ChatListener chatListener = new ChatListener();
		ChannelAPI channel = new ChannelAPI("http://localhost:8888", "key", chatListener);
		channel.open();
		*/
	}
	
	
	ChannelClient chnnelClient =  null;
	Handler handler = new Handler();

	private class MessageThread extends Thread {
		String msg = null;
		public MessageThread(String msg){
			this.msg = msg;
		}
		public void run(){
			inst.displayGAEMessage(this.msg);
		}
	}
		
		
	private class ChannelWorker extends Thread {
			GcmReportActivity repInst = null;
		public ChannelWorker(GcmReportActivity acti) {
			super();
			repInst = acti;
		}

		@Override
		public void run() {
			JSONObject result = null;
			URI appEngineURI = null;
			String token = null;
			try {
				appEngineURI = new URI("http://1-dot-henzmobile.appspot.com");
			} catch (Exception e) {
				e.printStackTrace();
			}
			// URI appEngineURI = new URI("http://localhost:8080/");
			
			HttpWorker mHttpWorker;
			
			mHttpWorker = new HttpWorker();
			
			
	        String url = "http://1-dot-henzmobile.appspot.com/channel/token.json";
	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("channelId", "henz.report");
	        
	        result = mHttpWorker.requestSingleJson(url, params);
			
	        //repInst.displayGAEMessage(String.valueOf(result));
	        
			try {
				token = result.getString("token");
			} catch (Exception e) {
				e.printStackTrace();
			}
	        
	        Util.Log(String.valueOf(result));
			Util.Log("token[" + token + "]");
			handler.post(new MessageThread("token[" + token + "]"));
			
			
			// Get a Channel token from the server
			chnnelClient = ChannelClient.createChannel(
					  appEngineURI
					, token
					, new ChannelClient.ChannelListener() {
					    @Override
					    public void onOpen() {
							handler.post(new MessageThread("channelOpened"));
							
					        String url = "http://1-dot-henzmobile.appspot.com/channel/sendChannel.json";
					        Map<String, Object> params = new HashMap<String, Object>();
					        params.put("channelId", "henz.report");
					        params.put("message", "android channel opened[" + repInst.getTitle() + "]");
					        
							HttpWorker mHttpWorker;
							
							mHttpWorker = new HttpWorker();
					        mHttpWorker.requestSingleJson(url, params);
					    }
					 
					    @Override
					    public void onMessage(String message) {
					    	//inst.displayGAEMessage(message);
							handler.post(new MessageThread(message));
					    }
					 
					    @Override
					    public void onError(int code,
					         String description) {
							handler.post(new MessageThread(code + ":" + description));
					    	//inst.displayGAEMessage(code + ":" + description);
					    }
					 
					    @Override
					    public void onClose() {
							handler.post(new MessageThread("Channel Close"));
					    	//inst.displayGAEMessage("Channel Close");
					    }
					  });
			try {
				chnnelClient.open();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}		
	}
	
}
